﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Bussiness;

namespace WebApplication.Admin
{
    public partial class XuatVo : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadMatHang();
            }
        }
        public void LoadMatHang()
        {
            IUnitOfWork iunitfwork = new EFUnitOfWork();
            MATHANGRepository repository = new MATHANGRepository(new EFRepository<MATHANG>(), iunitfwork);
            IQueryable<MATHANG> objmathang = repository.All();
            Dlmathang.DataSource = objmathang;
            Dlmathang.DataValueField = "MAMH";
            Dlmathang.DataTextField = "TENMH";
            Dlmathang.DataBind();
            Dlmathang.Items.Insert(0, new ListItem {Text="---Chọn mặt hàng---",Value="0" });
        }
        protected void lbtncapnhap_Click(object sender, EventArgs e)
        {
            tonkhovo();
        }
        public void tonkhovo()
        {
              string mamh = Dlmathang.SelectedValue;
            DateTime dt = Commomcs.convertDate(DateTime.Now.ToString("dd/MM/yyyy"));
            DateTime dtHtruoc = Commomcs.DuyenNgayThang(mamh);
            IUnitOfWork iunitfwork = new EFUnitOfWork();
            TONKHO_VORepository repositoryTonKhovo = new TONKHO_VORepository(new EFRepository<TONKHO_VO>(), iunitfwork);
            CHITIETHOADONXUATRepository repositoryChitietHDX = new CHITIETHOADONXUATRepository(new EFRepository<CHITIETHOADONXUAT>(), iunitfwork);
            HOADONNHAPRepository repositoryHdn = new HOADONNHAPRepository(new EFRepository<HOADONNHAP>(), iunitfwork);
            int Tongslxuat = 0;
            int Tongslnhap = 0;
            IQueryable<HOADONNHAP> LstHdX = repositoryHdn.All().Where(p => p.CHITIETHOADONNHAP.MAMH == "Dl19l" && p.NGAYNHAP == dt);
            IQueryable<CHITIETHOADONXUAT> LstHdN = repositoryChitietHDX.All().Where(p => p.HOADONXUAT.NGAYXUAT == dt && p.MAMH == "Dl19l");
            if (LstHdN != null)
            {
                foreach (CHITIETHOADONXUAT hdx in LstHdN)
                {
                    Tongslnhap += (int)hdx.SOLUONGVOVE;
                }
            }
            if (LstHdX != null)
            {
                foreach (HOADONNHAP hdn in LstHdX)
                {
                    Tongslxuat += (int)hdn.CHITIETHOADONNHAP.SOLUONGNHAP;
                }
            }
            TONKHO_VO Tonkhovo = null;
            TONKHO_VO TonkhoHt = null;
            bool iscapnhap = true;
            Tonkhovo = repositoryTonKhovo.All().Where(p => p.NGAYTHANG == dt && p.MAMH == "Dl19l").FirstOrDefault();
            if (Tonkhovo == null)
            {
                Tonkhovo = new TONKHO_VO();
                TonkhoHt = repositoryTonKhovo.All().Where(p => p.NGAYTHANG == dtHtruoc && p.MAMH == "Dl19l").FirstOrDefault();
                Tonkhovo.NGAYTHANG = dt;
                Tonkhovo.MAMH = Dlmathang.SelectedValue;
                if (TonkhoHt == null)
                {
                    Tonkhovo.SOLUONGVODAU = 0;
                    if (rdkolayvo.Checked)
                    {
                        Tonkhovo.SOLUONGVOXUAT = 0;
                        Tonkhovo.AMVO = -int.Parse(txtsoluong.Value);
                    }
                    if (rdlayvo.Checked)
                    {
                        Tonkhovo.SOLUONGVOXUAT = 0;
                        Tonkhovo.SOLUONGVOXUAT = -int.Parse(txtsoluong.Value);
                    }
                    Tonkhovo.SOLUONGVONHAP = Tongslnhap;
                    Tonkhovo.SOLUONGVOTONCUOI = Tongslnhap;
                    Tonkhovo.AMVO = 0;
                }
                if (TonkhoHt != null)
                {
                    Tonkhovo.SOLUONGVODAU = TonkhoHt.SOLUONGVOTONCUOI;
                    if (rdlayvo.Checked)
                    {
                        Tonkhovo.SOLUONGVOXUAT = int.Parse(txtsoluong.Value) ;
                    }
                    Tonkhovo.SOLUONGVONHAP = Tongslnhap;
                    Tonkhovo.SOLUONGVOTONCUOI = TonkhoHt.SOLUONGVOTONCUOI + Tongslnhap - Tongslxuat;

                }
            }
        }
    }
}